/*
 * @lc app=leetcode.cn id=720 lang=javascript
 *
 * [720] 词典中最长的单词
 */

// @lc code=start
/**
 * @param {string[]} words
 * @return {string}
 */
var longestWord = function (words) {
  const hash = new Set()
  hash.add('');
  let result = '';
  words.sort().forEach(word => {
    if (hash.has(word.slice(0, word.length - 1))) {
      hash.add(word);
      if (word.length > result.length) {
        result = word;
      }
    }
  })
  return result;
};
// @lc code=end
